from django.contrib import admin
from .models import Character, Skill, Monster, GameSession, CharacterSkill, Floor, Item, Shop, ShopItem

@admin.register(Character)
class CharacterAdmin(admin.ModelAdmin):
    list_display = ('name', 'user', 'level', 'gold', 'base_hp', 'base_attack', 'base_defense', 'created_at')
    list_filter = ('level', 'created_at')
    search_fields = ('name', 'user__username')

@admin.register(Skill)
class SkillAdmin(admin.ModelAdmin):
    list_display = ('name', 'skill_type', 'base_level', 'max_level')
    list_filter = ('skill_type',)
    search_fields = ('name', 'description')

@admin.register(Monster)
class MonsterAdmin(admin.ModelAdmin):
    list_display = ('name', 'monster_type', 'hp', 'attack', 'defense', 'reward_gold')
    list_filter = ('monster_type',)
    search_fields = ('name',)

@admin.register(GameSession)
class GameSessionAdmin(admin.ModelAdmin):
    list_display = ('character', 'current_floor', 'current_hp', 'is_active', 'started_at', 'ended_at', 'get_current_monsters')
    list_filter = ('is_active', 'started_at')
    search_fields = ('character__name',)

    def get_current_monsters(self, obj):
        return ", ".join([str(monster) for monster in obj.current_monsters.all()])
    get_current_monsters.short_description = '当前怪物'

@admin.register(CharacterSkill)
class CharacterSkillAdmin(admin.ModelAdmin):
    list_display = ('character', 'skill', 'current_level', 'is_equipped')
    list_filter = ('is_equipped', 'current_level')
    search_fields = ('character__name', 'skill__name')

@admin.register(Floor)
class FloorAdmin(admin.ModelAdmin):
    list_display = ('game_session', 'floor_number', 'floor_type', 'is_completed')
    list_filter = ('floor_type', 'is_completed')
    search_fields = ('game_session__character__name',)

@admin.register(Item)
class ItemAdmin(admin.ModelAdmin):
    list_display = ('name', 'item_type', 'price', 'effect_value')
    list_filter = ('item_type',)
    search_fields = ('name', 'description')

@admin.register(Shop)
class ShopAdmin(admin.ModelAdmin):
    list_display = ('floor',)
    search_fields = ('floor__game_session__character__name',)

@admin.register(ShopItem)
class ShopItemAdmin(admin.ModelAdmin):
    list_display = ('shop', 'item', 'quantity', 'is_sold')
    list_filter = ('is_sold',)
    search_fields = ('item__name',)
